package com.danskin.wechat.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.util.StringUtil;
import com.danskin.wechat.entity.SportsInfo;
import com.danskin.wechat.service.SportsInfoService;
import com.danskin.wechat.util.FTPHelper;
import com.danskin.wechat.util.Page;

@Controller
@RequestMapping("sportsInfo")
public class SportsInfoController extends BaseController{
	@Resource
	private SportsInfoService sportsInfoService;
	/***
	 * 我要运动List查询
	 * @param sportsInfo
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "querySportsInfoList", method = RequestMethod.GET)
	public String querySportsInfoList(SportsInfo sportsInfo,Model model){
		return "sportsInfo/sportsInfo_list";
	}
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getSportsInfoList", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object getSportsInfoList(Page page,HttpServletRequest request) throws Exception {
		Map map = getRequestParameter(this.getRequest());
		map.put("mpId", this.getMpId());
		page.setMap(map);
		List<SportsInfo> listSportsInfo = sportsInfoService.findSportsInfoList(page);
		return pagelistToJSONForBootStrapTable(listSportsInfo, page);
	}
	/***
	 * 删除我要运动
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "deleteSportsInfo", method = RequestMethod.POST)
	public void deleteSportsInfo(HttpServletRequest request,HttpServletResponse response){
		boolean flag = false;
		String id = request.getParameter("id");
		if(StringUtil.isNotEmpty(id)){
			sportsInfoService.deleteSportsInfo(id);
			flag = true;
		}
	    renderData(response, flag);
	}
	/***
	 * 修改我要运动
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "updateSportsInfo",method = RequestMethod.GET)
	public String updateSportsInfo(HttpServletRequest request,Model model){
		String id = request.getParameter("id");
		SportsInfo sportsInfo = null;
		if(StringUtil.isNotEmpty(id)){
			sportsInfo = sportsInfoService.findSportsInfobyId(id);
		}
		model.addAttribute("sportsInfo", sportsInfo);
		return "sportsInfo/updateSportsInfo";
	}
	/***
	 * 保存修改记录(我要运动)
	 * @param sportsInfo
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value = "saveUpdateSportsInfo",method = RequestMethod.POST)
	public String saveUpdateSportsInfo(HttpServletRequest request,SportsInfo sportsInfo,@RequestParam("picture")MultipartFile file) throws IllegalStateException, IOException{
		String id = String.valueOf(sportsInfo.getId());
		SportsInfo obj = null;
		String pictureUrl = null;
		if(!file.isEmpty()){
			 // 上传图片到ftp服务器并返回图片地址
	    	 pictureUrl = FTPHelper.getInstance().uploadPictureAndReturnPictureUrl(file, "sports",
	    			 FTPHelper.uploadTemporaryFile());
		}
		if(StringUtil.isNotEmpty(id)&&!"0".equals(id)){
			//修改
			obj = getSportsInfoUFT(sportsInfo,"1",pictureUrl);
			sportsInfoService.saveUpdateSportsInfo(obj);
		}else{
			//新增 
			obj = getSportsInfoUFT(sportsInfo,"2",pictureUrl);
			sportsInfoService.saveSportsInfo(obj);
		}
		return "redirect:/sportsInfo/querySportsInfoList";
	}
	public SportsInfo getSportsInfoUFT(SportsInfo sportsInfo,String type,String pathURL) throws UnsupportedEncodingException{
		SportsInfo sportsIn = new SportsInfo();
		if("1".equals(type)){
			sportsIn.setId(sportsInfo.getId());
		}
		sportsIn.setMpId(this.getLoginUser().getMpId());
		sportsIn.setTitle(sportsInfo.getTitle());
		sportsIn.setBannerUrl(sportsInfo.getBannerUrl());
		sportsIn.setImg(getConversionUTF(sportsInfo.getImg()));
		sportsIn.setClicks(sportsInfo.getClicks());
		sportsIn.setImg(pathURL);
		return sportsIn;
	}
	/***
	 * 新增我要运动
	 * @param sportsInfo
	 * @return
	 */
	@RequestMapping(value = "openSportsInfo",method = RequestMethod.GET)
	public String openSportsInfo(SportsInfo sportsInfo){
		return "sportsInfo/addSportsInfo";
	}
	private void renderData(HttpServletResponse response, boolean data) {
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(data);
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}
	public String getConversionUTF(String str) throws UnsupportedEncodingException{
		if(!"".equals(str)&&str!=null){
			str = new String(str.getBytes("iso-8859-1"), "UTF-8");
		}	
		return str;
	}
}
